package com.zyh.flume.intercept;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.util.ArrayList;
import java.util.List;

public class MyInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        byte[] body = event.getBody();
        if(body[0] >= 'a' && body[0] <= 'z'){
            event.getHeaders().put("type","letter");
            return event;
        }
        if(body[0] >= '0' && body[0] <= '9'){
            event.getHeaders().put("type","number");
            return event;
        }
        return null;
    }

    @Override
    public List<Event> intercept(List<Event> list) {
        List<Event> list2 = new ArrayList<>();
        for (Event event : list) {
            event = intercept(event);
            if(event != null){
                list2.add(event);
            }
        }
        return list2;
    }

    @Override
    public void close() {

    }

    public static class MyBuilder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new MyInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
